package com.xuexiang.xui_lib.component.alpha;

import com.xuexiang.xui_lib.util.AttrValue;
import com.xuexiang.xui_lib.util.LogUtils;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.hiviewdfx.HiLog;

import java.lang.ref.WeakReference;

/**
 * 透明度辅助工具
 */
public class XUIAlphaViewHelper implements IAlphaViewHelper {

    private WeakReference<Component> mTarget;

    /**
     * 设置是否要在 press 时改变透明度
     */
    private boolean mChangeAlphaWhenPress;

    /**
     * 设置是否要在 disabled 时改变透明度
     */
    private boolean mChangeAlphaWhenDisable;

    private float mNormalAlpha = 1F;
    private float mPressedAlpha;
    private float mDisabledAlpha;

    public XUIAlphaViewHelper(Component target, AttrSet attrSet) {
        mTarget = new WeakReference<>(target);
        mChangeAlphaWhenPress = AttrValue.get(attrSet, "xui_switch_alpha_pressed", true);
        mChangeAlphaWhenDisable = AttrValue.get(attrSet, "xui_switch_alpha_disabled", true);
        mPressedAlpha = AttrValue.get(attrSet, "xui_alpha_pressed", 0.5F);
        mDisabledAlpha = AttrValue.get(attrSet, "xui_alpha_disabled", 0.5F);
    }

    public XUIAlphaViewHelper(Component target, float pressedAlpha, float disabledAlpha) {
        mTarget = new WeakReference<>(target);
        mPressedAlpha = pressedAlpha;
        mDisabledAlpha = disabledAlpha;
    }

    /**
     * onPressedChanged
     *
     * @param current the view to be handled, maybe not equal to target view
     * @param pressed
     */
    @Override
    public void onPressedChanged(Component current, boolean pressed) {

        LogUtils.info("zhangliang==>", pressed + "");
        Component target = mTarget.get();
        if (target == null) {
            return;
        }
        if (current.isEnabled()) {
            target.setAlpha(mChangeAlphaWhenPress && pressed && current.isClickable() ? mPressedAlpha : mNormalAlpha);
        } else {
            if (mChangeAlphaWhenDisable) {
                target.setAlpha(mDisabledAlpha);
            }
        }
    }

    /**
     * onEnabledChanged
     *
     * @param current the view to be handled, maybe not  equal to target view
     * @param enabled
     */
    @Override
    public void onEnabledChanged(Component current, boolean enabled) {
        LogUtils.info("zhangliang==>", enabled + "");
        Component target = mTarget.get();
        if (target == null) {
            return;
        }
        float alphaForIsEnable;
        if (mChangeAlphaWhenDisable) {
            alphaForIsEnable = enabled ? mNormalAlpha : mDisabledAlpha;
        } else {
            alphaForIsEnable = mNormalAlpha;
        }
        if (current != target && target.isEnabled() != enabled) {
            target.setEnabled(enabled);
        }
        target.setAlpha(alphaForIsEnable);
    }

    /**
     * 设置是否要在 press 时改变透明度
     *
     * @param changeAlphaWhenPress 是否要在 press 时改变透明度
     */
    @Override
    public void setChangeAlphaWhenPress(boolean changeAlphaWhenPress) {
        mChangeAlphaWhenPress = changeAlphaWhenPress;
    }

    /**
     * 设置是否要在 disabled 时改变透明度
     *
     * @param changeAlphaWhenDisable 是否要在 disabled 时改变透明度
     */
    @Override
    public void setChangeAlphaWhenDisable(boolean changeAlphaWhenDisable) {
        mChangeAlphaWhenDisable = changeAlphaWhenDisable;
        Component target = mTarget.get();
        if (target != null) {
            onEnabledChanged(target, target.isEnabled());
        }
    }

}

